<?php

WebSignatureMatcher::register('_WebPathMatcher');
function _WebPathMatcher(IWebRequest $request, $sign, &$captures) {
	$pathInfo = $request->pathInfo();
	if (!$pathInfo) $pathInfo = '/';
	
	$path = @$sign[kWebPath];
	if (!is_null($path)) return $path == $pathInfo;
	
	$pathPattern = @$sign[kWebPathPattern];
	if (!is_null($pathPattern)) {
		$vars = null;
		$result = WebURLMatchesPattern($pathInfo, $pathPattern, $vars);

		if (!$result)
			return false;
		else {
			foreach ($vars as $name => $value)
				$captures[$name] = $value;
				
			return true;
		}
	} else
		return false;
}

WebSignatureMatcher::register('_WebInputMatcher');
function _WebInputMatcher(IWebRequest $request, $sign, &$captures) {
	foreach (array(
		kWebGET => $request->GETVariables(),
		kWebPOST => $request->POSTVariables(),
		kWebCookies => $request->cookies()) as $key => $values) {
		
		$toCapture = @$sign[$key]; if (!$toCapture) continue;
		foreach ($toCapture as $neededKey)
			$captures[$neededKey] = @$values[$neededKey];
	}
	
	return true;
}

WebSignatureMatcher::register('_WebMethodMatcher');
function _WebMethodMatcher(IWebRequest $request, $sign, &$captures) {
	$method = @$sign[kWebMethod];
	if ($method) {
		$currentMethod = @$request->method();
		$currentMethod = is_null($currentMethod) ? 'GET' : strtoupper($currentMethod);
		return strtoupper($method) == $currentMethod;			
	} else
		return true;
}

WebSignatureMatcher::registerValidator('_WebValidateMatcher');
function _WebValidateMatcher(IWebRequest $request, $sign, &$captures) {
	$validates = @$sign[kWebValidate];
	if (!$validates) return true;
	else {
		foreach ($validates as $name => $callable) {
			if (!call_user_func($callable, $captures[$name]))
				return false;
		}
		
		return true;
	}
}

?>